package com.cl.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class SendMailUtil {
    @Autowired
    JavaMailSenderImpl mailSender;
//    复杂的邮件
    public void sendMail(String subject,String text,String fileName,String filePathName,String toMail) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        {
            try {
                helper = new MimeMessageHelper(mimeMessage, true);
                helper.setSubject(subject);
                helper.setText(text, true);
                helper.addAttachment(fileName, new File(filePathName));
                helper.addAttachment(fileName, new File(filePathName));
                helper.setTo(toMail);
                helper.setFrom("2642801571@qq.com");
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }
        //发送附件
        mailSender.send(mimeMessage);
    }
//    简单的邮件
    public void sendSimpleMail(String subject,String text,String toMail){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(subject);
        message.setText(text);
        message.setTo(toMail);
        message.setFrom("2642801571@qq.com");
        mailSender.send(message);
    }
}
